﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class ZCService : RepositoryBase
    {
        public void AddZCApplication(ZCApplicationForm form)
        {
            InsertEntity(form);
        }

        public void AddZCMaterial(ZCMaterial material)
        {
            InsertEntity(material);
        }

        public void UpdateZCMaterial(ZCMaterial material)
        {
            UpdateEntity(material);
        }

        public void UpdateApplication(ZCApplicationForm form)
        {
            UpdateEntity(form);
        }

        public ZCApplicationForm GetApplication(long id)
        {
            ZCApplicationForm form = GetEntities<ZCApplicationForm>().Where(z => z.ID == id).FirstOrDefault();
            if (!form.UserReference.IsLoaded)
            {
                form.UserReference.Load();
            }
            if (!form.User1Reference.IsLoaded)
            {
                form.User1Reference.Load();
            }
            if (!form.User2Reference.IsLoaded)
            {
                form.User2Reference.Load();
            }
            if (!form.User3Reference.IsLoaded)
            {
                form.User3Reference.Load();
            }
            return form;
        }

        public int GetCountOfApplication(string prefix)
        {
            return GetEntities<ZCApplicationForm>().Where(s => s.ApplicationCode.StartsWith(prefix)).Count();
        }

        public IQueryable<ZCMaterial> GetMaterials(long id)
        {
            return GetEntities<ZCMaterial>().Where(m => m.ZCApplicationForm.ID == id);
        }

        public ZCMaterial GetMaterial(long id)
        {
            return GetEntities<ZCMaterial>().Where(z => z.ID == id).FirstOrDefault();
        }

        public void DeleteMaterial(long id)
        {
            DeleteEntities<ZCMaterial>(z=>z.ID == id, Guid.Empty);
        }

        public List<ZCApplicationForm> GetAllApplications(string type)
        {
            List<ZCApplicationForm> forms = new List<ZCApplicationForm>();
            IQueryable<ZCApplicationForm> entities = GetEntities<ZCApplicationForm>().Where(z => z.Type.Equals(type) && z.IsSubmit);
            foreach (ZCApplicationForm form in entities)
            {
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
                if (!form.User1Reference.IsLoaded)
                {
                    form.User1Reference.Load();
                }
                if (!form.User2Reference.IsLoaded)
                {
                    form.User2Reference.Load();
                }
                if (!form.User3Reference.IsLoaded)
                {
                    form.User3Reference.Load();
                }
                switch (form.Status)
                {
                    case "1":
                        form.Status = "等待第一审批人审批";
                        break;
                    case "2":
                        form.Status = "等待第二审批人审批";
                        break;
                    case "3":
                        form.Status = "等待第三审批人审批";
                        break;
                    case "4":
                        form.Status = "已审批";
                        break;
                    case "-1":
                        form.Status = "被驳回";
                        break;
                }
                forms.Add(form);
            }
            return forms;
        }

        public List<ZCApplicationForm> GetApplicationsToSign(string type)
        {
            List<ZCApplicationForm> forms = new List<ZCApplicationForm>();
            IQueryable<ZCApplicationForm> entities = GetEntities<ZCApplicationForm>().Where(z => z.Type.Equals(type) && z.IsSubmit && !z.Status.Equals("-1") && !z.Status.Equals("4"));
            foreach (ZCApplicationForm form in entities)
            {
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
                if (!form.User1Reference.IsLoaded)
                {
                    form.User1Reference.Load();
                }
                if (!form.User2Reference.IsLoaded)
                {
                    form.User2Reference.Load();
                }
                if (!form.User3Reference.IsLoaded)
                {
                    form.User3Reference.Load();
                }
                switch (form.Status)
                {
                    case "1":
                        form.Status = "等待第一审批人审批";
                        break;
                    case "2":
                        form.Status = "等待第二审批人审批";
                        break;
                    case "3":
                        form.Status = "等待第三审批人审批";
                        break;
                    case "4":
                        form.Status = "已审批";
                        break;
                    case "-1":
                        form.Status = "被驳回";
                        break;
                }
                forms.Add(form);
            }
            return forms;
        }

        public List<ZCApplicationForm> GetApplicationsToSign(string type, long userID)
        {
            List<ZCApplicationForm> forms = new List<ZCApplicationForm>();
            IQueryable<ZCApplicationForm> entities = GetEntities<ZCApplicationForm>().Where(z => z.Type.Equals(type) && z.IsSubmit &&
                (z.User2.UserID == userID || z.User3.UserID == userID || z.User1.UserID == userID) && !z.Status.Equals("-1") && !z.Status.Equals("4"));
            foreach (ZCApplicationForm form in entities)
            {
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
                if (!form.User1Reference.IsLoaded)
                {
                    form.User1Reference.Load();
                }
                if (!form.User2Reference.IsLoaded)
                {
                    form.User2Reference.Load();
                }
                if (!form.User3Reference.IsLoaded)
                {
                    form.User3Reference.Load();
                }
                switch (form.Status)
                {
                    case "1":
                        form.Status = "等待第一审批人审批";
                        break;
                    case "2":
                        form.Status = "等待第二审批人审批";
                        break;
                    case "3":
                        form.Status = "等待第三审批人审批";
                        break;
                    case "4":
                        form.Status = "已审批";
                        break;
                    case "-1":
                        form.Status = "被驳回";
                        break;
                }
                forms.Add(form);
            }
            return forms;
        }
    }
}
